package com.trainbook.entity.VO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainbook.entity.Seat;
import com.trainbook.entity.Site2site;
import com.trainbook.entity.Train;
import com.trainbook.entity.TrainSites;
import com.trainbook.service.ISite2siteService;
import com.trainbook.service.ITrainService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Dempsey
 * @Date: 2020/11/30 16:13
 * @Description:
 */
@Data
@ApiModel(value = "trainVO", description = "用于给前端返回车次的信息列表")
public class TrainVO {
    // 车次名列表、出发时间、到达时间、历时、中间站点、提供等级、票价

    @ApiModelProperty(value = "出发时间")
    private LocalDateTime startTime;

    @ApiModelProperty(value = "到达时间")
    private LocalDateTime endTime;

    @ApiModelProperty(value = "历时")
    private List<Long> takeTime;

    @ApiModelProperty(value = "车次名列表")
    private Map<Integer,String> trainNameMap;

    @ApiModelProperty(value = "中间站点")
    private List<TrainSites> trainSitesList;

    @ApiModelProperty(value = "提供等级")
    private List<Seat> seatList;

    @ApiModelProperty(value = "总路程")
    private Double allDistance;

    @ApiModelProperty(value = "票价")
    private Double price;

    public void setTakeTime(){
        Duration duration = Duration.between(this.startTime,this.endTime);
        List<Long> durationTimes = new ArrayList<>();
        durationTimes.add(duration.toDays());
        durationTimes.add(duration.toHours());
        durationTimes.add(duration.toMinutes());
        durationTimes.add(duration.toMillis());
        this.takeTime = durationTimes;
    }

    @Autowired
    private ISite2siteService site2siteService;
    public void setPrice(){
        for (int i = 1; i < this.trainSitesList.size(); i++) {
            int siteA = this.trainSitesList.get(i-1).getSiteId();
            int siteB = this.trainSitesList.get(i).getSiteId();
            Site2site one = site2siteService.getOne(new QueryWrapper<Site2site>().eq("siteA", siteA).eq("siteB", siteB)
                    .or().eq("siteA", siteB).eq("siteB", siteA));
            this.price += one.getPrice();
        }
    }

    public void setAllistance(){
        for (int i = 1; i < this.trainSitesList.size(); i++) {
            int siteA = this.trainSitesList.get(i-1).getSiteId();
            int siteB = this.trainSitesList.get(i).getSiteId();
            Site2site one = site2siteService.getOne(new QueryWrapper<Site2site>().eq("siteA", siteA).eq("siteB", siteB)
                    .or().eq("siteA", siteB).eq("siteB", siteA));
            this.allDistance += one.getDistance();
        }
    }

    @Autowired
    private ITrainService trainService;
    public void setTrainNameMap(){
        for (TrainSites trainSites : this.trainSitesList) {
            Integer id = trainSites.getId();
            Train train = trainService.getOne(new QueryWrapper<Train>().eq("id", id));
            this.trainNameMap.put(id,train.getTrainName());
        }
    }

}
